<template>
    <div class="manage-notice">
        <div style="height: 35px">
            <el-form :model="searchForm" :inline="true" size="mini">
                <el-form-item label="name">
                    <el-input v-model="searchForm.name"></el-input>
                </el-form-item>
                <el-form-item label="code">
                    <el-input v-model="searchForm.code"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="listNotice">Search</el-button>
                    <el-button type="primary" @click="listNotice">Refresh</el-button>
                    <el-button type="primary" @click="handleCreate">New</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
                v-infiniteScroll="infiniteScroll"
                border
                default-expand-all
                highlight-current-row
                :data="noticeTable"
                row-key="ukId"
                height="500"
                size="mini"
                style="width: 100%"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="title"
                    label="title"
                    width="*">
            </el-table-column>
            <el-table-column
                    show-overflow-tooltip
                    prop="content"
                    label="content"
                    width="230">
            </el-table-column>
            <el-table-column
                    show-overflow-tooltip
                    prop="releaseTime"
                    label="releaseTime"
                    width="220">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="status"
                    width="155">
                <template slot-scope="scope">
                    <el-switch
                            @change="statusChange($event, scope.row.ukId)"
                            :value="scope.row.status != '1'"
                            active-color="#909399"
                            inactive-color="#67c23a"
                            active-text="未发布"
                            inactive-text="发布">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="145">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row.ukId)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
                width="60%"
                title="通知公告管理"
                :visible.sync="showNoticeForm"
                :close-on-click-modal="false"
                label-width="80px">
            <el-row>
                <el-col :span="18">
                    <el-form ref="form" :model="noticeForm" label-width="80px">
                        <el-form-item label="标题">
                            <el-input v-model="noticeForm.title"></el-input>
                        </el-form-item>
                        <el-form-item label="内容">
                            <el-input
                                    type="textarea"
                                    :autosize="{ minRows: 10, maxRows: 50}"
                                    maxlength="500"
                                    v-model="noticeForm.content">
                            </el-input>
                        </el-form-item>
                        <el-form-item label="状态">
                            <el-select v-model="noticeForm.status" placeholder="请选择状态">
                                <el-option
                                        v-for="item in this.$store.state.Pabu.dict.manage.ARTICLE.dictItems"
                                        :key="item.ukId"
                                        :label="item.name"
                                        :value="parseInt(item.value)">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleSave">保存</el-button>
                            <el-button @click="showNoticeForm = false">取 消</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="6">
                    机构用户树
                </el-col>
            </el-row>
        </el-dialog>
    </div>
</template>

<script>
  import {deleteNotice, listNotice, saveNotice, updateNoticeStatus} from "../../../api/manage/notice";
  import PageUtils from "../../../utils/PageUtils";

  export default {
    name: "Notice",
    data () {
      return {
        showNoticeForm: false,
        noticeTable: [],
        searchForm: {},
        noticeForm: {},
        pagination: {
          total: 0,
          size: 30,
          current: 1,
        }
      }
    },
    methods: {
      handleCreate () {
        this.noticeForm = {
          status: 1
        }
        this.showNoticeForm = true
      },
      handleEdit (row) {
        this.noticeForm = row
        this.showNoticeForm = true
      },
      handleSave () {
        saveNotice(this.noticeForm).then(result => {
          this.listNotice()
          this.showNoticeForm = false
        }).catch(error => {
          console.log(error)
        })
      },
      handleDelete (ukId) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteNotice(ukId).then(result => {
            this.listNotice()
            this.showNoticeForm = false
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(error => {
            console.log(error)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      statusChange (value, paginationId) {
        let status = value ? 0 : 1
        updateNoticeStatus(paginationId, status).then(result => {
          this.listNotice()
        }).catch(error => {
          console.log(error)
        })
      },
      infiniteScroll () {
        let current = this.$store.state.Pabu.pagination.notice.current + 1
        let size = this.$store.state.Pabu.pagination.notice.size
        listNotice(this.searchForm, current, size).then(result => {
          if (result.data.notices.records.length !== 0) {
            for (let notice of result.data.notices.records) {
              this.noticeTable.push(notice)
            }
            PageUtils.setPagination(this, "notice", result.data.notices)
          }
        }).catch(error => {
          console.log(error)
        })
      },
      listNotice () {
        listNotice(this.searchForm, 1, 30).then(result => {
          this.noticeTable = result.data.notices.records
          PageUtils.setPagination(this, "notice", result.data.notices)
        }).catch(error => {
          console.log(error)
        })
      }
    },
    created () {
      this.listNotice()
    }
  }
</script>

<style scoped>
    .manage-notice {
        width: 100%;
        height: 100%;
    }

    /* element ui样式重写 */
    .manage-notice > .el-table {
        height: calc(100% - 35px) !important;
    }

    .manage-notice > .el-table > .el-table__body-wrapper {
        height: calc(100% - 35px) !important;
    }
</style>